package io.renren.modules.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.shop.entity.*;
import io.renren.modules.shop.mapper.*;
import io.renren.modules.shop.service.GoodsAndOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class GoodsAndOrderServiceImpl extends ServiceImpl<GoodsAndOrderMapper, GoodsAndOrder> implements GoodsAndOrderService {

    @Autowired
    private GoodsAndOrderMapper goodsAndOrderMapper;

    @Autowired
    private GoodsOrderMapper goodsOrderMapper;

    @Autowired
    private WxUserMapper wxUserMapper;

    @Autowired
    private GoodsMsgMapper goodsMsgMapper;

    @Autowired
    private GoodsCouponMapper goodsCouponMapper;

    @Autowired
    private UserAndCouponMapper userAndCouponMapper;

    @Override
    public List<GoodsAndOrder> getPage(GoodsAndOrder goodsAndOrder, Integer size, Integer page) {
        //参数一是当前页，参数二是每页个数
        IPage<GoodsAndOrder> iPage = new Page<>(page, size);
        IPage<GoodsAndOrder> resultPge = goodsAndOrderMapper.selectPage(iPage, new QueryWrapper<>(goodsAndOrder));
        return resultPge.getRecords();
    }

    @Override
    public boolean addCar(GoodsMsg goodsMsg, String openId, Integer num) {
        // 查询购物车表中的当前用户下是否存在此商品
        GoodsAndOrder goods = goodsAndOrderMapper.selectOne(new QueryWrapper<GoodsAndOrder>().lambda()
                .eq(GoodsAndOrder::getGoodsStatus, 0) // 没有被购买
                .eq(GoodsAndOrder::getGoodsId, goodsMsg.getGoodsId()) // 商品存在
                .eq(GoodsAndOrder::getOpenid, openId));
        // 如果商品不存在
        if (null == goods) {
            goods = new GoodsAndOrder();
            goods.setOpenid(openId);
            goods.setGoodsAllPrice(goodsMsg.getGoodsPrice().multiply(BigDecimal.valueOf(num)));
            goods.setGoodsId(goodsMsg.getGoodsId());
            goods.setGoodsNum(num);
            goods.setGoodsPrice(goodsMsg.getGoodsPrice());
            goodsAndOrderMapper.insert(goods);
        } else {
            goods.setGoodsNum(goods.getGoodsNum() + num);
            goods.setGoodsAllPrice(goods.getGoodsAllPrice().add(goods.getGoodsPrice().multiply(BigDecimal.valueOf(goods.getGoodsNum()))));
            goodsAndOrderMapper.updateById(goods);
        }
        return true;
    }

    @Override
    public boolean delCar(GoodsMsg goodsMsg, String openid, Integer num) {
        GoodsAndOrder goodsAndOrder = goodsAndOrderMapper.selectOne(new QueryWrapper<GoodsAndOrder>().lambda()
                .eq(GoodsAndOrder::getGoodsStatus, 0) // 没有被购买
                .eq(GoodsAndOrder::getGoodsId, goodsMsg.getGoodsId()) // 商品存在
                .eq(GoodsAndOrder::getOpenid, openid));
        goodsAndOrder.setGoodsNum(goodsAndOrder.getGoodsNum() - num);
        int i = goodsAndOrderMapper.updateById(goodsAndOrder);
        return i > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void buyOne(GoodsMsg goodsMsg, String openId, Integer num, Integer goodsCouponId) {

        // 查询当前购买物品，修改数量
        goodsMsg.setGoodsNum(goodsMsg.getGoodsNum() - num);
        // 货物数量为0时，修改状态为缺货
        if (goodsMsg.getGoodsNum() == 0) {
            goodsMsg.setGoodsStatus(1);
        }
        goodsMsgMapper.updateById(goodsMsg);
        // 插入订单详情
        GoodsAndOrder goodsAndOrder = new GoodsAndOrder();
        goodsAndOrder.setOpenid(openId);
        goodsAndOrder.setGoodsId(goodsMsg.getGoodsId());
        goodsAndOrder.setGoodsNum(num);
        goodsAndOrder.setGoodsPrice(goodsMsg.getGoodsPrice());
        goodsAndOrder.setGoodsAllPrice(goodsMsg.getGoodsPrice().multiply(BigDecimal.valueOf(num)));
        goodsAndOrder.setGoodsStatus(1);
        goodsAndOrderMapper.insert(goodsAndOrder);
        //创建总订单数据
        GoodsOrder goodsOrder = new GoodsOrder();
        goodsOrder.setOpenid(openId);
        goodsOrder.setOrderAllPrice(BigDecimal.valueOf(0));
        goodsOrder.setOrderBargainPrice(BigDecimal.valueOf(0));
        goodsOrder.setOrderEndPrice(BigDecimal.valueOf(0));
        goodsOrderMapper.insert(goodsOrder);
        // 查询是否满足优惠券条件

        // 订单总表中插入商品数据
//        goodsOrder.setGoodsAndOrderId(goodsAndOrder.getGoodsAndOrderId());
        goodsOrder.setOrderAllPrice(goodsOrder.getOrderAllPrice().add(goodsAndOrder.getGoodsPrice().multiply(BigDecimal.valueOf(goodsAndOrder.getGoodsNum()))));
        goodsOrder.setOrderEndPrice(goodsOrder.getOrderEndPrice().add(goodsAndOrder.getGoodsPrice().multiply(BigDecimal.valueOf(goodsAndOrder.getGoodsNum()))));
        goodsOrder.setCouponId(goodsCouponId);
        // 设置为待付款状态
        goodsOrder.setOrderStatus(1);
        // 检查是否有优惠券可使用
        if(goodsCouponId != 0) {
            GoodsCoupon goodsCoupon = new GoodsCoupon();
            goodsCoupon = goodsCouponMapper.selectById(goodsCouponId);
            goodsOrder.setOrderBargainPrice(goodsCoupon.getCouponSize());
            goodsOrder.setOrderEndPrice(goodsOrder.getOrderEndPrice().subtract(goodsCoupon.getCouponSize()));
            // 创建优惠券和用户的实体类（用于保存优惠券使用数据）
            UserAndCoupon userAndCoupon = new UserAndCoupon();
            userAndCoupon.setCouponId(goodsCouponId);
            userAndCoupon.setOpenid(openId);
            userAndCouponMapper.insert(userAndCoupon);
        }
        goodsOrderMapper.updateById(goodsOrder);

    }

    @Override
    public int updateBatchSelective(List<GoodsAndOrder> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public boolean cancelOrder(Integer goodsOrderId) {
        GoodsOrder goodsOrder = goodsOrderMapper.selectById(goodsOrderId);
        // 查询获得当前订单下的所有商品详情
        List<GoodsAndOrder> goodsAndOrderList = goodsAndOrderMapper.selectList(new QueryWrapper<GoodsAndOrder>().lambda().eq(GoodsAndOrder::getOrderId, goodsOrder.getOrderId()));
        // 循环，将所有商品信息还原
        for (GoodsAndOrder goodsAndOrder : goodsAndOrderList) {
            GoodsMsg goodsMsg = goodsMsgMapper.selectById(goodsAndOrder.getGoodsId());
            goodsMsg.setGoodsNum(goodsMsg.getGoodsNum() + goodsAndOrder.getGoodsNum());
            goodsMsg.setGoodsStatus(0);
            goodsMsgMapper.updateById(goodsMsg);
        }
        goodsOrder.setOrderStatus(-1);
        goodsOrderMapper.updateById(goodsOrder);
    return true;
    }

}







